import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'
import { message } from 'antd'
import * as taskAction from './task'
import { convertContent } from '../constant/lang-util'

export const addAssignment = (taskId, newAssignment, callback) => {
  return () => {
    (async () => {
      const res = await request.post(`../api/v2/assignments`, newAssignment)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('创建成功'))
        callback()
      }
    })()
  }
}

export const getAssignments = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/tasks/${taskId}/assignments`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({
          type: 'GET_ASSIGNMENTS',
          assignments: res.body
        })
      }
    })()
  }
}

export const getQuizzes = (type, page = 1, pageSize = 10, searchConditions = {}) => {
  const {id, userId, description, tags = []} = searchConditions
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/assignments/quizzes?type=${type}&page=${--page}&pageSize=${pageSize}
      ${id ? '&quizId=' + id : ''}
      ${userId ? '&createId=' + userId : ''}
      ${description ? '&description=' + description : ''}
      ${tags.length ? '&tags=' + tags : ''}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({
          type: 'GET_ASSIGNMENT_QUIZZES',
          data: res.body
        })
      }
    })()
  }
}

export const addAssignmentQuiz = (selectedRowKeys, taskId, assignmentId) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/v2/tasks/${taskId}/assignments/${assignmentId}/quizzes`, selectedRowKeys)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('添加成功'))
      }
    })()
  }
}

export const addAssignmentInfo = (assignment) => {
  return () => {
    (async () => {
      await request.update(`../api/v2/assignments/${assignment.id}`, assignment)
    })()
  }
}
export const getAssignment = (assignmentId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/assignments/${assignmentId}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({
          type: 'GET_ASSIGNMENT',
          data: res.body
        })
      }
    })()
  }
}

export const deleteAssignment = (assignmentId, taskId, callback) => {
  return dispatch => {
    (async () => {
      const res = await request.del(`../api/v2/assignments/${assignmentId}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        callback()
      }
    })()
  }
}

export const getQuizzesByIds = (quizType, quizIds, callback) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/assignments/quizzesByIds?type=${quizType}&ids=${quizIds.join(',')}`)
      const quizzes = quizzesSort(res.body, quizIds)
      if (res.status === HTTP_CODE.OK) {
        callback()
        dispatch({
          type: 'GET_ASSIGNMENT_QUIZZES_BY_QUIZ_IDS',
          data: quizzes
        })
      }
    })()
  }
}

export const dragSort = (data, taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/v2/assignments`, data)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(getAssignments(taskId))
      }
    })()
  }
}
export const dragSortQuiz = (data, taskId, assignmentId) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/assignments/${assignmentId}/assignmentsQuiz`, data)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(taskAction.getTask(taskId))
      }
    })()
  }
}

export const changeAssignmentVisible = (taskId, assignmentId, visibility) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/v2/assignments/${assignmentId}/visible`, {visibility})
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(taskAction.getTask(taskId))
      }
    })()
  }
}
export const changeAssignmentsVisible = (id, visibility) => {
  return dispatch => {
    (async () => {
      const res = await request.update(`../api/v2/tasks/${id}/assignments/visible`, {visibility})
      if (res.status === HTTP_CODE.NO_CONTENT) {
        dispatch(taskAction.getTask(id))
      }
    })()
  }
}

const quizzesSort = (data, quizIds) => {
  let quizzesSort = []
  quizIds.forEach(quizId => {
    data.forEach(quiz => {
      if (quiz.id === quizId) {
        quizzesSort.push(quiz)
      }
    })
  })
  return quizzesSort
}
